package org.itboys.mobile.controller.site;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.JoinFavorite;
import org.itboys.mobile.service.mongo.site.JoinSiteFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/9/26
 * 描述：雄安加盟网_关注
 */
@Controller
@RequestMapping("/join/site/favorite")
public class JoinSiteFavoriteController extends BaseController{
    @Autowired
    private JoinSiteFavoriteService favoriteService;

    /**
     * 关注
     * @param request
     * @param response
     * @param memberRecordId
     * @param otherId
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="memberRecordId",required=true)Long memberRecordId,
                     @RequestParam(value="otherId",required=true)Long otherId
                    ){
        try {
            favoriteService.save(new JoinFavorite(memberRecordId,otherId));
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/favorite/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }

    }

    /**
     * 取消关注
     * @param request
     * @param response
     * @param memberRecordId
     * @param otherId
     */
    @RequestMapping("/cancel")
    public void cancel(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="memberRecordId",required=true)Long memberRecordId,
                     @RequestParam(value="otherId",required=true)Long otherId
    ){
        try {
            favoriteService.cancel(memberRecordId,otherId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/join/site/favorite/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }

    }

}
